package com.recruit.g1.company.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.recruit.g1.common.response.JsonResult;
import com.recruit.g1.company.mapper.CompanyMapper;
import com.recruit.g1.pojo.company.dto.CompanyAddParam;
import com.recruit.g1.pojo.company.dto.CompanyListQuery;
import com.recruit.g1.pojo.company.entity.Company;
import com.recruit.g1.pojo.company.vo.CompanyListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Validated
@Slf4j//nei
@Api(tags = "企业详情管理模块")
@RestController
@RequestMapping("/v1/company/")
public class CompanyController {

    @Autowired
    private CompanyMapper companyMapper;


    @GetMapping("count/")
    @ApiOperation("查询企业数量")
    public JsonResult count() {
        int count = companyMapper.selectCount();
        return JsonResult.ok(count);
    }

    @GetMapping("detail/{id}")
    @ApiOperation("查询企业详情")
    public JsonResult selectCompany(@PathVariable Integer id) {
//        Company company = new Company();
        Company company = companyMapper.select(id);
        return JsonResult.ok(company);
    }
    /**
     * 1.添加企业详情
     */
    @PostMapping("add")
    @ApiOperation("添加企业信息")
    @ApiOperationSupport(order = 10)
    public JsonResult insertCompany(@RequestBody CompanyAddParam companyAddParam) {
        log.debug("companyAddParam ="+ companyAddParam);
        //获取用户信息(登陆状态拦截)
        //User userSaveParam =(User) session.getAttribute("currentLoginUser");//////////////////////////////////////////////////////
        /*if (userSaveParam == null) {
            log.error("User not logged in or session expired.");
            return JsonResult.error("User not logged in or session expired.");
        }*/
        //登陆状态
        Company company = new Company();
        BeanUtils.copyProperties(companyAddParam, company);
        log.debug("company ="+ company);
        companyMapper.insert(company);

        return JsonResult.ok();
    }

    @GetMapping("select")
    public List<CompanyListVO> queryCompanyList(CompanyListQuery queryDTO) {
        return companyMapper.selectCompanyList(queryDTO);
    }

    @GetMapping("list")
    @ApiOperation("查询企业信息")
    @ApiOperationSupport(order = 20)
    public JsonResult listCompany(){
        List<Company> companyList =companyMapper.selectAll();
        return JsonResult.ok(companyList);
    }
}
